package pattern.behavioral.iterator.simple;

/**
 * @author: chaoyang.ren  
 * @date:Jun 5, 2017  
 * @time:11:35:50 AM   
 * @email:chaoyang.ren@foxmail.com  
 * @version: 1.0
 */
public class MyIterator<E> implements Iterator<E> {
	private Collection<? extends E> collection;
	private volatile int index;
	private int size;
	
	public MyIterator(Collection<? extends E> collection){
		E[] arrs = collection.toArray();
		this.collection = new MyCollection<E>(arrs);
		this.size = arrs.length;
		this.index = 0;
	}

	public E first() {
		this.index = 0;
		return collection.getElement(index);
	}

	public E next() {
		return collection.getElement(index++);
	}

	public boolean hasNext() {
		if(size == index){
			return false;
		}
		return true;
	}

	public Collection<? extends E> getCollection() {
		return collection;
	}

	public void setCollection(Collection<? extends E> collection) {
		this.collection = collection;
	}

	public int size() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

}

